package com.example.yichuangyun.mvvm.ui.activity.view;

import androidx.lifecycle.MutableLiveData;

import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.mvvm.base.BaseViewModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseNetCallback;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MultipartBody;

public class ImproveCompanyInfoViewModel extends BaseViewModel {

    public MutableLiveData<List<String>> urls = new MutableLiveData<>();
    public MutableLiveData<String> uploadError = new MutableLiveData<>();
    public MutableLiveData<String> infoSuccess = new MutableLiveData<>();
    public MutableLiveData<String> infoError = new MutableLiveData<>();

    /**
     * 多图上传
     */
    public void uploadImages(MultipartBody.Part[] parts) {
        addSubscribe(mRetrofitHelper.uploadImage(parts).subscribeWith(new BaseNetCallback<List<String>>() {

            @Override
            public void onSuccess(BaseModel<List<String>> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    urls.setValue(baseModel.getData());
                } else {
                    uploadError.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                uploadError.setValue(message);
            }
        }));
    }

    /**
     * 完善企业信息
     *
     * @param enterpriseEid      企业eid
     * @param enterpriseCreditNo 企业信用代码
     * @param infoContent        信息内容
     * @param infoSource         信息来源
     */
    public void saveCompanyInfo(String enterpriseEid, String enterpriseCreditNo, int improveType, String infoContent, Integer infoSource, List<String> urls) {
        Map<String, Object> map = new HashMap<>();
        map.put("enterpriseEid", enterpriseEid);
        map.put("enterpriseCreditNo", enterpriseCreditNo);
        map.put("infoContent", infoContent);
        map.put("improveType", improveType);
        map.put("infoSource", infoSource);
        map.put("fileUrlList", urls);
        addSubscribe(mRetrofitHelper.saveCompanyInfo(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<String>() {

            @Override
            public void onSuccess(BaseModel<String> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    infoSuccess.setValue(baseModel.getData());
                } else {
                    infoError.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                infoError.setValue(message);
            }
        }));
    }
}
